// ==UserScript==
// @name          Ikariam - Ally Highlight
// @description   Version 1.2 - Colors the players of user defined alliances in the highscore list, and colors the alliance itself in the alliance highscore. Now also shows alliance members in island view.
// @version       1.2
// @date          2008-11-20
// @namespace     archeorbiter.googlecode.com
// @include       http://s*.ikariam.tld/index.php*
// @include       http://s*.ikariam.com.*/index.php*
// @include       http://s*.*.ikariam.com/index.php*
// @include       http://s*.ikariam.co.il/index.php*
// @require       http://ecmanaut.googlecode.com/svn/trunk/lib/gm/$x$X.js
// ==/UserScript==


// Clean alliance list
function cleanList(allyList) {
	if (allyList != '') allyList = allyList.replace(/,\s+/g,',').split(',');
	else allyList = [];
	return allyList;
}

// Config values
var showPlayer = GM_getValue(location.host +'_showplayer', true);
var showAlly = GM_getValue(location.host +'_showally', true);
var showIsland = GM_getValue(location.host +'_showisland', true);
var friendly = cleanList(GM_getValue(location.host +'_friendly', ''));
var hostile = cleanList(GM_getValue(location.host +'_hostile', ''));
var fColor = GM_getValue(location.host +'_fcolor', '#C9E0FF');
var hColor = GM_getValue(location.host +'_hcolor', '#FFC9C9');
	

var page = document.getElementsByTagName('body')[0].id;

if (page == 'highscore' || 'allyHighscore' || 'island') {

	// Style function
	function color(ally, target) {
		function iterate(allyArray, color) {
			for (var i = 0; i < allyArray.length; i++) {
				if (ally == allyArray[i] && ally != '' && ally != '-') {
					target.style.backgroundColor = color;
					if (page == 'island') target.textContent = target.parentNode.textContent;
				}
			}
		}
		iterate(hostile, hColor);
		iterate(friendly, fColor);
	}
	
	if (page == 'highscore' && showPlayer == true || page == 'allyHighscore' && showAlly == true) {
		function getName(elem) {
			var ally = (page == 'highscore') ? elem.innerHTML : elem.innerHTML.match(/\((.*?)\)/)[1];
			var target = elem.parentNode.parentNode;
			color(ally, target);
			}
		$x('//a[@class="allyLink"]').forEach(getName);
	}
	
	if (page == 'island' && showIsland == true) {
		function getName(elem) {
			var ally = $X('ul[@class="cityinfo"]/li[@class="ally"]', elem).childNodes[1].textContent;
			var label = $X('a[starts-with(@id,"city_")]/span[@class="textLabel"]', elem);
			var width = document.defaultView.getComputedStyle(label, null).getPropertyValue('width');
			width = parseInt(width.match(/(\d+)/)[1]) + 18;
			var target = document.createElement('span');
			target.className = 'hh_overlay';
			target.setAttribute('style', 'position: absolute; top: 4px; left: 8px; display: block; width: '+ width +'px; height: 15px; line-height: 15px');
			label.appendChild(target);
			color(ally, target);
			}
		$x('//li[starts-with(@class,"cityLocation city level")]').forEach(getName);
	}
}


// Add options
if (page == 'options') {
	
	var mainView = document.getElementById('mainview');
	var gameSettings = mainView.childNodes[6];

	// Make a new section and add the form
	var hhOptions = document.createElement('div');
	hhOptions.id = 'ah_options';
	hhOptions.className = 'contentBox01h';
	hhOptions.innerHTML =
		'<h3 class="header">' +
			'<span class="textLabel">Alliance Highlighter Options</span>' +
		'</h3>' +
		'<div class="content">' +
			'<table cellspacing="0" cellpadding="0">' +
				'<tr>' +
					'<th>Friendly alliances <br>(Comma separated list of tags)</th>' +
					'<td><input id="ah_friendly" class="textfield" type="textfield" value="'+ friendly +'" size="30"/></td>' +
				'</tr>' + 
				'<tr>' +
					'<th>Hostile alliances <br>(Comma separated list of tags)</th>' +
					'<td><input id="ah_hostile" class="textfield" type="textfield" value="'+ hostile +'" size="30"/></td>' +
				'</tr>' +
				'<tr>' +
					'<th>Highlight player highscore</th>' +
					'<td><input id="ah_showplayer" type="checkbox" value="'+ showPlayer +'"/></td>' +
				'</tr>' +
				'<tr>' +
					'<th>Highlight alliance highscore</th>' +
					'<td><input id="ah_showally" type="checkbox" value="'+ showAlly +'"/></td>' +
				'</tr>' +
				'<tr>' +
					'<th>Highlight in island view</th>' +
					'<td><input id="ah_showisland" type="checkbox" value="'+ showIsland +'"/></td>' +
				'</tr>' +
			'</table>' +
			'<h3>Change colors</h3>' +
			'<table cellspacing="0" cellpadding="0">' +
				'<tr>' +
					'<th>Friendly alliance color</th>' +
					'<td><input id="ah_fcolor" class="textfield" type="textfield" value="'+ fColor +'" size="10"/></td>' +
				'</tr>' +
				'<tr>' +
					'<th>Hostile alliance color</th>' +
					'<td><input id="ah_hcolor" class="textfield" type="textfield" value="'+ hColor +'" size="10"/></td>' +
				'</tr>' +
			'</table>' +
		'</div>' +
		'<div class="footer"></div>';
	mainView.insertBefore(hhOptions, gameSettings.nextSibling);

	// Make checkboxes persistent
	(showPlayer == true) ? document.getElementById('ah_showplayer').setAttribute('checked','checked') : document.getElementById('ah_showplayer').removeAttribute('checked');
	(showAlly == true) ? document.getElementById('ah_showally').setAttribute('checked','checked') : document.getElementById('ah_showally').removeAttribute('checked');
	(showIsland == true) ? document.getElementById('ah_showisland').setAttribute('checked','checked') : document.getElementById('ah_showisland').removeAttribute('checked');

	// Event listeners for the form
	document.getElementById('ah_friendly').addEventListener('change',function(event){GM_setValue(location.host +'_friendly',document.getElementById('ah_friendly').value)},false);
	document.getElementById('ah_hostile').addEventListener('change',function(event){GM_setValue(location.host +'_hostile',document.getElementById('ah_hostile').value)},false);
	document.getElementById('ah_showplayer').addEventListener('change',function(event){GM_setValue(location.host +'_showplayer',document.getElementById('ah_showplayer').checked)},false);
	document.getElementById('ah_showally').addEventListener('change',function(event){GM_setValue(location.host +'_showally',document.getElementById('ah_showally').checked)},false);
	document.getElementById('ah_showisland').addEventListener('change',function(event){GM_setValue(location.host +'_showisland',document.getElementById('ah_showisland').checked)},false);
	document.getElementById('ah_fcolor').addEventListener('change',function(event){GM_setValue(location.host +'_fcolor',document.getElementById('ah_fcolor').value)},false);
	document.getElementById('ah_hcolor').addEventListener('change',function(event){GM_setValue(location.host +'_hcolor',document.getElementById('ah_hcolor').value)},false);
}
